package com.haierCamera.customapplication.utils;

import android.annotation.TargetApi;
import android.os.Build;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    @TargetApi(Build.VERSION_CODES.N)
    public static String getSevenDayBefore(){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - 604800);
        String shur_time = dateFormat.format(calendar.getTime());
//        Log.e("TAG","7天前是什么时间:"+shur_time);
        return shur_time;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static String getSeveralBefore(int d){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - 24 * 60 * 60 *(d));
        String shur_time = dateFormat.format(calendar.getTime());
//        Log.e("TAG", "几天前是什么时间:"+shur_time);
        return shur_time;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static String getNowTime(){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
        String shur_time = dateFormat.format(calendar.getTime());
//        Log.e("TAG","今天是什么时间:"+shur_time);
        return shur_time;
    }

    public static String getNowTime2(){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 ");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
        String shur_time = dateFormat.format(calendar.getTime());
//        Log.e("TAG","今天是什么时间:"+shur_time);
        return shur_time;
    }

    public static int getDayofWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(System.currentTimeMillis()));

        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static String getDayString(int week) {
        if (week == 1) {
            return "星期日";
        } else if (week == 2) {
            return "星期一";
        } else if (week == 3) {
            return "星期二";
        } else if (week == 4) {
            return "星期三";
        } else if (week == 5) {
            return "星期四";
        } else if (week == 6) {
            return "星期五";
        } else if (week == 7) {
            return "星期六";
        }
        return "";
    }

    public static String getDateToString(long milSecond, String pattern) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 获得两个日期间距多少天
     *
     * @return
     */
    public static int dateDiff(String startTime, String endTime, String format) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        int day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = (int)(diff / nd);// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
            System.out.println("时间相差：" + day + "天" + hour + "小时" + min
                    + "分钟" + sec + "秒。");
            if (day>=1) {
                return day;
            }else {
                if (day==0) {
                    return 1;
                }else {
                    return 0;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }

}
